package com.one.common.view.pop;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.one.common.R;
import com.one.common.utils.PopupAlphaAnimatorUtil;
import com.one.common.utils.ScreenUtils;


/**
 * 拍照、相册popupwindow
 */
public class CameraAndAlbumPopup {
    private Activity activity = null;
    private PopupWindow popupWindow = null;
    private View contentView = null;

    public CameraAndAlbumPopup(Activity activity) {
        this.activity = activity;
    }

    /**
     * 认证弹出的拍照popupwindow
     * 不想修改提示文字，第二个参数传null即可
     */
    public CameraAndAlbumPopup initPop(final OnClickCameraOrAlbumListener listener) {
        popupWindow = new PopupWindow(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        LayoutInflater inflater = LayoutInflater.from(activity);
        contentView = inflater.inflate(R.layout.pop_camera_album, null);
        TextView tvTakePhoto = contentView.findViewById(R.id.tv_take_photo);// 拍照
        TextView tvAlbum = contentView.findViewById(R.id.tv_album);// 相册
        TextView tvCancel = contentView.findViewById(R.id.tv_cancel);// 取消

        tvCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                closePop();
            }
        });
        tvTakePhoto.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                closePop();
                listener.onClickCameraButton();
            }
        });
        tvAlbum.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                closePop();
                listener.onClickAlbumButton();
            }
        });

        popupWindow.setContentView(contentView);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        popupWindow.setAnimationStyle(R.style.bottom_dialog_anim);
        popupWindow.setClippingEnabled(false);     // 可以超出屏幕，状态栏终于可以变半透明啦

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                PopupAlphaAnimatorUtil.setOutAlphaAnim(activity);
            }
        });

        return this;
    }

    public void showPop(View parent) {
        PopupAlphaAnimatorUtil.setInAlphaAnim(activity);
        popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);

        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) contentView.getLayoutParams();
        layoutParams.setMargins(0, 0, 0, ScreenUtils.getNavigationBarHeight(activity));
    }

    /**
     * 关闭
     */
    public void closePop() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
    }

    /**
     * 点击事件回调
     */
    public interface OnClickCameraOrAlbumListener
    {
        public void onClickCameraButton();

        public void onClickAlbumButton();
    }
}
